package com.cjbdi.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cjbdi.entity.UserSubscribe;
import com.cjbdi.dao.CourtSubscribeRepository;
import com.cjbdi.dao.SubscribeRepository;
import com.cjbdi.entity.CourtSubscribe;
import com.cjbdi.entity.Report;
import com.cjbdi.entity.Subscribe;
import com.cjbdi.entity.UserInfo;
import com.cjbdi.pagination.PageInfo;
import com.cjbdi.service.SubscribeService;
import com.cjbdi.service.UserSubscribeService;
import com.cjbdi.util.NumUtil;
import com.cjbdi.util.PropertiesUtil;
import com.cjbdi.util.StringEditor;
import com.cjbdi.vo.Result;

@Controller
@RequestMapping("subscribe")
public class SubscribeController {
	@Autowired
    private UserSubscribeService userSubscribeService;
	
	@Autowired
    private SubscribeService subscribeService;
	
	@Autowired
    private CourtSubscribeRepository courtSubscribeRepository;
    
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> page(@RequestParam(value = "limit", required = false, defaultValue = "10") String limit,
			@RequestParam(value = "offset", required = false, defaultValue = "0") String offset,UserSubscribe subscribe) {
    	Sort sort = new Sort(Direction.DESC,"id");
    	Pageable pageable = PageRequest.of(Integer.valueOf(offset)/Integer.valueOf(limit), Integer.valueOf(limit), sort);
		PageInfo<UserSubscribe> pageInfo = userSubscribeService.getPageByParam(subscribe.getStatus()==null?"":subscribe.getStatus(), subscribe.getFymc()==null?"":subscribe.getFymc(), subscribe.getSfunction()==null?"":subscribe.getSfunction(),pageable);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("count", pageInfo.getRecords());
		result.put("queryData", pageInfo.getRows());
		return result;
    }
    
    @RequestMapping(value ="/updateSubscribeStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result updateSubscribeStatus(int id,String status){
    	List<UserSubscribe> list = userSubscribeService.getSubscribeByid(id);
    	if(list.size()>0) {
    		UserSubscribe subscribe = (UserSubscribe)list.get(0);
    		subscribe.setStatus(status);
    		userSubscribeService.save(subscribe);
    		String function = subscribe.getSfunction();
    		List<CourtSubscribe> courtSubscribelist = courtSubscribeRepository.findByFybm(subscribe.getFybm());
    		if(courtSubscribelist.size()>0) {
    			CourtSubscribe courtSubscribe = courtSubscribelist.get(0);
    			if(status.equals("开通")) {
        			courtSubscribe.setSfunction(courtSubscribe.getSfunction()+","+function);
    			}else {
    				String[] str = courtSubscribe.getSfunction().split(",");
    				List<String> tmp = Arrays.asList(str);
    				List<String> arrayList=new ArrayList<String>(tmp);//转换为ArrayLsit调用相关的remove方法
    		        arrayList.remove(function);
    		        String result = StringUtils.join(arrayList.toArray(), ",");
    		        courtSubscribe.setSfunction(result);

    			}
    				
    			
    			courtSubscribeRepository.save(courtSubscribe);
    		}
    		return new Result(200,"更新用户订阅信息成功",list);
    	}else {
    		return new Result(901,"获取用户订阅信息失败",null);
    	}
    }
   
    @RequestMapping(value ="/findAllSubscribe", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result findAllSubscribe(){
    	List<Subscribe> list = subscribeService.findAll();
    	return new Result(200,"成功获取所有可订阅功能",list);
    }
    
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result save(Subscribe subscribe) {
    	subscribe.setUpdateTime(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    	subscribeService.save(subscribe);
        return new Result(200,"新增订阅功能成功",null);
    	
    }
    
    @RequestMapping(value = "/edit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result edit(int id) {
    	List<Subscribe> list = subscribeService.findById(id);
    	if(list.size()>0) {
    		return new Result(200,"获取订阅功能成功",list.get(0));
    	}else {
        	return new Result(903,"获取订阅功能失败",null);

    	}
    	
    }
    
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
	public Result update(Subscribe subscribe) {
    	List<Subscribe> list = subscribeService.findById(subscribe.getId());
    	if(list.size()>0) {
    		Subscribe src = list.get(0);
    		src.setUpdateTime(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    		src.setDescription(subscribe.getDescription());
    		subscribeService.save(src);
    		return new Result(200, "修改订阅功能成功", null);
    	}else {
    		return new Result(905, "修改订阅功能失败", null);
    	}
	}
    
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(int id,String name){
    	List<CourtSubscribe> list = courtSubscribeRepository.findByName(name);
    	if(list.size()>0) {
    		return new Result(905,"该功能已经被订阅！不能删除！",null);
    	}else {
    		boolean b = subscribeService.delete(id);
        	if(b) {
        		return new Result(200,"删除订阅功能成功",null);
    		}else {
    			return new Result(904,"删除订阅功能失败",null);
    		}
    	}
    	
    }
    
    @RequestMapping(value = "/pageFunction", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> pageFunction(@RequestParam(value = "limit", required = false, defaultValue = "10") String limit,
			@RequestParam(value = "offset", required = false, defaultValue = "0") String offset,UserSubscribe subscribe) {
    	Sort sort = new Sort(Direction.DESC,"id");
    	Pageable pageable = PageRequest.of(Integer.valueOf(offset)/Integer.valueOf(limit), Integer.valueOf(limit), sort);
		PageInfo<Subscribe> pageInfo = subscribeService.getPageByParam(subscribe.getSfunction()==null?"":subscribe.getSfunction(),pageable);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("count", pageInfo.getRecords());
		result.put("queryData", pageInfo.getRows());
		return result;
    }
    
    @RequestMapping(value = "/courtSubscribeSave", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result courtSubscribeSave(CourtSubscribe courtSubscribe) {
    	List<CourtSubscribe> list = courtSubscribeRepository.findByFybm(courtSubscribe.getFybm());
    	if(list.size()>0) {
    		CourtSubscribe src = list.get(0);
    		src.setSfunction(courtSubscribe.getSfunction());
    		src.setCreated(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    		courtSubscribeRepository.save(src);
    	}else {
    		courtSubscribe.setCreated(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        	courtSubscribeRepository.save(courtSubscribe);
    	}
        return new Result(200,"授权成功",null);
    	
    }
    
    @RequestMapping(value = "/findByFybm", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result findByFybm(String fybm) {
    	List<CourtSubscribe> list = courtSubscribeRepository.findByFybm(fybm);
    	if(list.size()>0) {
    		return new Result(200,"获取授权信息",list.get(0).getSfunction());
    	}else {
    		return new Result(902,"此法院未授权",null);
    	}
        
    	
    }

}
